<template>
  <el-dialog
    v-model="visibleDialog"
    title="物料号选择"
    style="min-width: 850px"
    draggable
    @close="closeDialog"
    destroy-on-close
    align-center
  >
    <el-form :model="searchParam" :inline="true">
      <el-form-item label="物料名称" prop="materialName">
        <el-input
          v-if="!props.isReadonly"
          v-model="searchParam.materialName"
          placeholder="请输入物料名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="物料编号">
        <el-input
          v-model="searchParam.materialCode"
          placeholder="请输入物料编号"
          clearable
          @keyup.enter="search"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search"
          ><i-ep-search />搜索</el-button
        >
        <el-button @click="reset1"><i-ep-refresh />重置</el-button>
      </el-form-item>
    </el-form>

    <div class="formTable1">
      <el-table
        ref="singleTableRef"
        :data="tableData"
        style="width: 100%"
        :key="Math.random"
        highlight-current-row
        row-key="id"
        @current-change="handleCurrentChange1"
        border
      >
        <el-table-column
          label="物料名称"
          prop="materialName"
          :align="'center'"
          min-width="50"
        />
        <el-table-column
          label="物料编号"
          prop="materialCode"
          :align="'center'"
          min-width="50"
        />
      </el-table>
    </div>

    <el-pagination
      :background="true"
      :current-page="pageable.pageNum"
      :page-size="pageable.pageSize"
      :page-sizes="[5, 10, 15, 20]"
      :total="pageable.total"
      layout="total, sizes, prev, pager, next, jumper"
      class="mt-[20px]"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit"
          >确 定
        </el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import { ElMessage, ElForm } from "element-plus";
import { ref, reactive, watch, onBeforeUnmount } from "vue";
import request from "@/utils/request";
import dayjs from "dayjs";
defineOptions({
  name: "EAMGRANTRECORDSELECT",
  inheritAttrs: false,
});

const emit = defineEmits(["change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  partnerId: {
    type: String,
    default: () => {
      return "";
    },
  },
  breakList: {
    type: Array,
    default: () => {
      return [];
    },
  },
});

//获取表格数据 api 方法
const requestApi = "/srm/core/partnerproduct/materialListByPage";
//获取数据初始化参数
const initParam = {};
// 初始化搜索参数数据
// 初始化搜索参数数据
const searchParamInit = {
  materialName: "",
  materialCode: "",
  partnerId: props.partnerId,
};
// 是否需要分页
const pagination = true;
// 无需二次数据处理&不传赞数也行
const tableData1 = ref([]);
const dataCallBack = (data: any) => {
  return data;
};
// const dataCallBack = ref(undefined);
// 数据请求错误后处理
function requestError() {}
//删除
const deleteParam = {
  api: "",
  data: {},
};

const {
  tableData,
  pageable,
  ids,
  searchParam,
  reset,
  search,
  getTableList,
  handleSizeChange,
  handleSelectionChange,
  handleTableDelete,
  handleCurrentChange,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack,
  requestError,
  deleteParam
);

const visibleDialog = ref(false);
let partnerId = ref("");
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    searchParam.value.partnerId = props.partnerId;
    getTableList();

    if (props.breakList.length > 0) {
      breakSingleTableSelect();
    }
  }
);

// 选择高亮
const singleTableRef = ref();
const currentRow = ref();
const handleCurrentChange1 = (val: any) => {
  currentRow.value = val;
};

// 回显时设置选项
const breakSingleTableSelect = () => {
  nextTick(() => {
    tableData.value.forEach((item, index) => {
      if (item.materialId == props.breakList[0].materialId) {
        // singleTableRef.value.toggleRowSelection(item, true); // 回显

        //   //遍历回显数组
        singleTableRef.value!.setCurrentRow(item);
        //   checkedrow.forEach((row) => {
        //     //singleTableRef这个是在table上面加的ref的值  通过它的属性方法去看那几个属性打钩
        //     singleTableRef.value.toggleRowSelection(row, true); // 回显
      }
    });
  });
};

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
}

/** email消息发送记录保存提交 */
function handleSubmit() {
  if (currentRow.value) {
    emit("change-visible", false, currentRow.value);
    currentRow.value = {};
  } else {
    return ElMessage.warning("请选择一条数据");
  }
}

// 重置表单
const reset1 = () => {
  searchParam.value.materialName = "";
  searchParam.value.materialCode = "";
  getTableList();
};

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

onMounted(() => {});
//产品列表
</script>

<style scoped lang="scss">
:deep(.el-table th .cell) {
  color: black;
  font-weight: 700;
}
.progress {
  color: red;
}
.success {
  color: rgb(100, 207, 86);
}
</style>
